package com.Servlet;

import com.Util.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@WebServlet(urlPatterns = {"/upload"})
public class upload extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        String username = req.getSession().getAttribute("username").toString();
        InputStream inputstream = req.getPart("portrait").getInputStream();
        UserDao.updateportrait(username,inputstream);


        if(UserDao.getImage(username) != null)
            req.getSession().setAttribute("portrait", UserDao.getImage(username));
        try {
            System.out.println("about to dispatch");
            req.getRequestDispatcher("profile.jsp").forward(req, res);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }

}
